#[cfg(test)]
mod tests {
    use super::*;
    use crate::commands::*;
    use crate::types::*;
    use tokio;

    #[tokio::test]
    async fn test_greet_command() {
        let result = utils::greet("World").unwrap();
        assert!(result.success);
        assert!(result.data.is_some());
        assert!(result.data.unwrap().contains("World"));
    }

    #[tokio::test]
    async fn test_system_info_command() {
        let result = utils::get_system_info().await.unwrap();
        assert!(result.success);
        assert!(result.data.is_some());
        
        let system_info = result.data.unwrap();
        assert!(!system_info.os.is_empty());
        assert!(!system_info.arch.is_empty());
    }

    #[tokio::test]
    async fn test_workspace_validation() {
        // 测试无效路径
        let result = utils::validate_workspace("non_existent_path".to_string()).await.unwrap();
        assert!(result.success);
        
        let validation = result.data.unwrap();
        assert!(!validation.is_valid);
        assert!(!validation.issues.is_empty());
    }

    #[tokio::test]
    async fn test_codegraph_initialization() {
        let workspace = std::env::current_dir().unwrap().to_string_lossy().to_string();
        let result = codegraph::initialize_codegraph(workspace).await.unwrap();
        
        assert!(result.success);
        assert!(result.data.is_some());
        
        let graph_id = result.data.unwrap();
        assert!(!graph_id.is_empty());
    }

    #[tokio::test]
    async fn test_lsp_server_lifecycle() {
        // 启动 LSP 服务器
        let start_result = lsp::start_lsp_server("rust".to_string()).await.unwrap();
        assert!(start_result.success);
        
        let server_id = start_result.data.unwrap();
        assert!(!server_id.0.is_empty());
        
        // 停止 LSP 服务器
        let stop_result = lsp::stop_lsp_server(server_id).await.unwrap();
        assert!(stop_result.success);
    }

    #[tokio::test]
    async fn test_file_operations() {
        let test_dir = std::env::temp_dir().join("codegraph_test");
        let test_dir_str = test_dir.to_string_lossy().to_string();
        
        // 创建测试目录
        let create_result = filesystem::create_directory(test_dir_str.clone()).await.unwrap();
        assert!(create_result.success);
        
        // 读取目录
        let read_result = filesystem::read_directory(test_dir_str.clone()).await.unwrap();
        assert!(read_result.success);
        
        // 清理测试目录
        let delete_result = filesystem::delete_file_or_directory(test_dir_str).await.unwrap();
        assert!(delete_result.success);
    }

    #[tokio::test]
    async fn test_progress_manager() {
        use crate::services::progress::PROGRESS_MANAGER;
        
        let operation_id = PROGRESS_MANAGER.create_operation("test_operation".to_string());
        assert!(!operation_id.is_empty());
        
        // 更新进度
        let update_result = PROGRESS_MANAGER.update_progress(&operation_id, 50, 100, "测试进度".to_string());
        assert!(update_result.is_ok());
        
        // 获取操作状态
        let operation = PROGRESS_MANAGER.get_operation(&operation_id);
        assert!(operation.is_some());
        
        let op = operation.unwrap();
        assert_eq!(op.operation_type, "test_operation");
        assert!(op.progress.is_some());
        
        // 完成操作
        let complete_result = PROGRESS_MANAGER.complete_operation(&operation_id, serde_json::json!({"result": "success"}));
        assert!(complete_result.is_ok());
    }

    #[tokio::test]
    async fn test_file_watcher() {
        use crate::services::watcher::WATCHER_SERVICE;
        
        let test_dir = std::env::temp_dir().join("codegraph_watcher_test");
        std::fs::create_dir_all(&test_dir).unwrap();
        let test_dir_str = test_dir.to_string_lossy().to_string();
        
        // 开始监控
        let watcher_result = WATCHER_SERVICE.start_watching(&test_dir_str).await;
        assert!(watcher_result.is_ok());
        
        let watcher_id = watcher_result.unwrap();
        assert!(!watcher_id.is_empty());
        
        // 停止监控
        let stop_result = WATCHER_SERVICE.stop_watching(&watcher_id).await;
        assert!(stop_result.is_ok());
        
        // 清理测试目录
        std::fs::remove_dir_all(&test_dir).unwrap();
    }

    #[tokio::test]
    async fn test_language_detection() {
        use crate::utils;
        
        assert_eq!(utils::detect_language_from_extension("rs"), "rust");
        assert_eq!(utils::detect_language_from_extension("py"), "python");
        assert_eq!(utils::detect_language_from_extension("js"), "javascript");
        assert_eq!(utils::detect_language_from_extension("ts"), "typescript");
        assert_eq!(utils::detect_language_from_extension("unknown"), "plaintext");
        
        assert!(utils::is_source_file("main.rs"));
        assert!(utils::is_source_file("app.py"));
        assert!(!utils::is_source_file("README.md"));
        
        assert!(utils::should_ignore_file("node_modules"));
        assert!(utils::should_ignore_file("target"));
        assert!(utils::should_ignore_file(".git"));
        assert!(!utils::should_ignore_file("src/main.rs"));
    }

    #[tokio::test]
    async fn test_error_handling() {
        use crate::error::*;
        
        let error = TauriCommandError::file_system("测试错误");
        assert!(matches!(error, TauriCommandError::FileSystem { .. }));
        
        let response: CommandResponse<String> = CommandResponse::error("测试错误");
        assert!(!response.success);
        assert!(response.error.is_some());
        assert!(response.data.is_none());
        
        let success_response = CommandResponse::success("测试数据".to_string());
        assert!(success_response.success);
        assert!(success_response.data.is_some());
        assert!(success_response.error.is_none());
    }

    #[tokio::test]
    async fn test_type_definitions() {
        use crate::types::*;
        
        let node_id = NodeId::new_v4();
        let edge_id = EdgeId::new_v4();
        
        assert_ne!(node_id, NodeId::new_v4());
        assert_ne!(edge_id, EdgeId::new_v4());
        
        let position = Position { row: 10, column: 5 };
        assert_eq!(position.row, 10);
        assert_eq!(position.column, 5);
        
        let source_location = SourceLocation {
            file_path: "test.rs".to_string(),
            start_line: 1,
            start_column: 0,
            end_line: 1,
            end_column: 10,
            byte_offset: 0,
            byte_length: 10,
        };
        assert_eq!(source_location.file_path, "test.rs");
    }
}
